//
//  WeatherViewController.m
//  Today
//
//  Created by fanfan on 15/5/22.
//  Copyright (c) 2015年 fanfan. All rights reserved.
//

#import "WeatherViewController.h"

@interface WeatherViewController ()

@end

@implementation WeatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"切换城市" style:UIBarButtonItemStylePlain target:self action:@selector(change)];
    [self loadinformation];
    // Do any additional setup after loading the view.
}
- (void)Citynumber:(NSInteger)number{
    self.str = [NSString stringWithFormat:@"http://m.weather.com.cn/atad/%ld.html", number];
    self.str = [self.str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:self.str];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    self.dataArray = [NSMutableArray array];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic1 = [dic objectForKey:@"weatherinfo"];
        WeatherModel *model = [WeatherModel getWeatherModel];
        model.city = [dic1 objectForKey:@"city"];
        model.weather1 = [dic1 objectForKey:@"weather1"];
        model.weather2 = [dic1 objectForKey:@"weather2"];
        model.weather3 = [dic1 objectForKey:@"weather3"];
        model.weather4 = [dic1 objectForKey:@"weather4"];
        
        model.temp1 = [dic1 objectForKey:@"temp1"];
        model.temp2 = [dic1 objectForKey:@"temp2"];
        model.temp3 = [dic1 objectForKey:@"temp3"];
        model.temp4 = [dic1 objectForKey:@"temp4"];
        
        model.wind1 = [dic1 objectForKey:@"wind1"];
        model.wind2 = [dic1 objectForKey:@"wind2"];
        model.wind3 = [dic1 objectForKey:@"wind3"];
        model.wind4 = [dic1 objectForKey:@"wind4"];
        
        model.fl1 = [dic1 objectForKey:@"fl1"];
        model.fl2 = [dic1 objectForKey:@"fl2"];
        model.fl3 = [dic1 objectForKey:@"fl3"];
        model.fl4 = [dic1 objectForKey:@"fl4"];
        
        model.index_d = [dic1 objectForKey:@"index_d"];
        [self.dataArray addObject:model];
        [self initview];
    }];

}
- (void)change{
    ChangeCityViewController *city = [[ChangeCityViewController alloc] init];
    city.mydelegate = self;
    [self.navigationController pushViewController:city animated:NO];
}
- (void)loadinformation{
    self.str = [NSString stringWithFormat:@"http://m.weather.com.cn/atad/101010100.html"];
    self.str = [self.str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:self.str];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    self.dataArray = [NSMutableArray array];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic1 = [dic objectForKey:@"weatherinfo"];
        WeatherModel *model = [WeatherModel getWeatherModel];
        model.city = [dic1 objectForKey:@"city"];
        model.weather1 = [dic1 objectForKey:@"weather1"];
        model.weather2 = [dic1 objectForKey:@"weather2"];
        model.weather3 = [dic1 objectForKey:@"weather3"];
        model.weather4 = [dic1 objectForKey:@"weather4"];
        
        model.temp1 = [dic1 objectForKey:@"temp1"];
        model.temp2 = [dic1 objectForKey:@"temp2"];
        model.temp3 = [dic1 objectForKey:@"temp3"];
        model.temp4 = [dic1 objectForKey:@"temp4"];
        
        model.wind1 = [dic1 objectForKey:@"wind1"];
        model.wind2 = [dic1 objectForKey:@"wind2"];
        model.wind3 = [dic1 objectForKey:@"wind3"];
        model.wind4 = [dic1 objectForKey:@"wind4"];
        
        model.fl1 = [dic1 objectForKey:@"fl1"];
        model.fl2 = [dic1 objectForKey:@"fl2"];
        model.fl3 = [dic1 objectForKey:@"fl3"];
        model.fl4 = [dic1 objectForKey:@"fl4"];
        
        model.index_d = [dic1 objectForKey:@"index_d"];
        [self.dataArray addObject:model];
        [self initview];
    }];
}
- (void)initview{
    weatherTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height ) style:UITableViewStylePlain];
    weatherTableView.delegate = self;
    weatherTableView.dataSource = self;
    weatherTableView.bounces = NO;
    weatherTableView.showsHorizontalScrollIndicator = NO;
    weatherTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:weatherTableView];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifier = @"weather";
    WeatherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (cell == nil) {
        cell = [[WeatherTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    WeatherModel *model = [self.dataArray lastObject];

    cell.backgroundView.contentMode = UIViewContentModeScaleAspectFit;
    
    cell.citylabel.text = model.city;
    cell.temp1label.text = model.temp1;
    cell.temp2label.text = model.temp2;
    cell.temp3label.text = model.temp3;
    cell.temp4label.text = model.temp4;
    
    cell.weather1label.text = model.weather1;
    cell.weather2label.text = model.weather2;
    cell.weather3label.text = model.weather3;
    cell.weather4label.text = model.weather4;
    
    cell.wind1label.text = model.wind1;
    cell.wind2label.text = model.wind2;
    cell.wind3label.text = model.wind3;
    cell.wind4label.text = model.wind4;
    
    cell.fl1label.text = model.fl1;
    cell.fl2label.text = model.fl2;
    cell.fl3label.text = model.fl3;
    cell.fl4label.text = model.fl4;
    
    cell.index_dlabel.text = model.index_d;
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.frame.size.height * 2;
}
- (void)viewWillAppear:(BOOL)animated{
    [weatherTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
